import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_li_ke_jie/entity/app_pay_result.dart';
import 'package:flutter_li_ke_jie/router/router.dart';
import 'package:flutter_li_ke_jie/ui/home/vip_model.dart';
import 'package:flutter_li_ke_jie/ui/login/login_model.dart';

import '../../constant/app_constant.dart';
import '../../style/common_color.dart';
import '../../utils/img_util.dart';
import '../../utils/navigator_util.dart';

///支付结果页面
class PayResultPage extends StatefulWidget {
  final AppPayResult appPayResult;//支付查询数据
  const PayResultPage(this.appPayResult,{Key? key}) : super(key: key);
  @override
  _PayResultPageState createState() => _PayResultPageState();
}

class _PayResultPageState extends State<PayResultPage>  with SingleTickerProviderStateMixin{
  String img = "pay_loading.gif";//当前动画
  String text = "支付结果查询中.....";//当前动画
  bool showBtn = false;//显示按钮
  @override
  void initState() {

    super.initState();
    // 查询用户支付状态
    _queryPayResult().then((payState){
      switch(payState){
        case 1:{
          //交易创建，等待买家付款
          setState(() {
            img = "pay_wait.gif";
            text = "交易创建成功，等待买家付款";
            showBtn = true;
          });
          break;
        }
        case 2:{
          //交易成功
          //更新下用户的信息 主要是vip信息
          LoginModel().getUserInfo().then((value){
            setState(() {
              img = "pay_success.gif";
              text = "支付成功";
              showBtn = true;
            });
          });
          break;
        }
        case 3:{
          //支付处理中 继续查询
          setState(() {
            img = "pay_loading.gif";
            text = "支付结果查询中.....";
          });
          _queryPayResult();
          break;
        }
        case 4:{
          //交易失败
          setState(() {
            img = "pay_fail.gif";
            text = "支付失败，如有疑惑,请联系客服:${service_phone}";
            showBtn = true;
          });
          break;
        }
        case 5:{
          //交易已经关闭
          setState(() {
            img = "pay_fail.gif";
            text = "改交易订单已经关闭,请重新返回支付页面重新下单，如有疑惑,请联系客服:${service_phone}";
            showBtn = true;
          });
          break;
        }
      }
    });

  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child:Scaffold(
          appBar: _appBar(),
          body: Container(
            color: Colors.white,
            width: MediaQuery.of(context).size.width,
            height: double.infinity,
            alignment: Alignment.center,
            child:  Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(height: MediaQuery.of(context).size.height/7,),
                Image.asset(ImageUtil.loadLocalImg(img),width: 120,height: 120,),
                SizedBox(height: 20,),
                Padding(padding: EdgeInsets.only(left: 30,right: 30),child:  Text(text,style: TextStyle(color: color_1e,fontSize: 20,),textAlign:TextAlign.center,),),
                SizedBox(height: 40,),
                Visibility(child:  MaterialButton(
                  height: 50,
                  minWidth:MediaQuery.of(context).size.width/3*2,
                  color: themeColor,
                  shape:const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                  ) ,
                  onPressed: (){
                    NavigatorUtil.goAndFinishAll(context, HOME_PAGE);
                  },
                  child: Text("返回首页",style: TextStyle(color: Colors.white,fontSize: 16),),
                ),visible: showBtn,)
              ],
            ),
          ),
        ), onWillPop: (){ return Future.value(false);});
  }

  AppBar _appBar(){
    return AppBar(
      backgroundColor: Colors.white,
      elevation: 0,
      title: Text("支付结果查询"),
      centerTitle: true,
      titleTextStyle: TextStyle(color: color_1e,fontSize: 18,fontWeight: FontWeight.bold),
      leadingWidth: 40,
    );
  }

  Future<int> _queryPayResult() async{
    return await new VipModel().queryPayResult(widget.appPayResult.outTradeNo, widget.appPayResult.shopdate,widget.appPayResult.vipNum).then((result){
      if(result!=null&&result.payState>0){
        return Future.value(result.payState);
      }
      return Future.value(3);//
    });
  }

}
